package com.sky.datasource;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.github.pagehelper.PageHelper;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

@Configuration  
@MapperScan(basePackages = {"com.sky.web.data2.*.dao"}, sqlSessionTemplateRef = "data2SqlSessionTemplate")
public class TestMyBatisConfig2 {
    @Bean(name = "data2DataSource")  
	 public DataSource testDataSource(DBConfig2 testConfig) throws SQLException{
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();  
		mysqlXaDataSource.setUrl(testConfig.getUrl());  
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);  
		mysqlXaDataSource.setPassword(testConfig.getPassword());  
		mysqlXaDataSource.setUser(testConfig.getUsername());  
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);  
		
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();  
        xaDataSource.setXaDataSource(mysqlXaDataSource);  
        xaDataSource.setUniqueResourceName("data2DataSource");  
  
        xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());  
        xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());  
        xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());  
        xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());  
        xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());  
        xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());  
        xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());  
        xaDataSource.setTestQuery(testConfig.getTestQuery());  
        return xaDataSource; 
	 }
	 
	    @Bean(name = "data2SqlSessionFactory")  
	    @Primary 
	    public SqlSessionFactory testSqlSessionFactory(@Qualifier("data2DataSource") DataSource dataSource)throws Exception {  
	        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();  
	        bean.setDataSource(dataSource);  
	        //分页插件
	        PageHelper pageHelper = new PageHelper();
	        Properties properties = new Properties();
	        properties.setProperty("reasonable", "true");
	        properties.setProperty("supportMethodsArguments", "true");
	        properties.setProperty("returnPageInfo", "check");
	        properties.setProperty("params", "count=countSql");
	        pageHelper.setProperties(properties);
	        
	        //mybatis配置文件
	        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	        try {
	            bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/data2/*/*.xml"));
	            return bean.getObject();
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new RuntimeException(e);
	        }
	    }  
	  
	    @Bean(name = "data2SqlSessionTemplate")  
	    @Primary 
	    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("data2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {  
	        return new SqlSessionTemplate(sqlSessionFactory);  
	    }  
}
